package com.weiaibenpao.demo.chislim.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.weiaibenpao.demo.chislim.R;

/**
 * Created by Administrator on 2017/12/19.
 */

public class LongClickToFinishView extends View {
    private static final float MAX_PROGRESS = 100f;
    // 画实心圆的画笔
    private Paint mCirclePaint;
    // 画圆环的画笔
    private Paint mRingPaint;
    // 画文字的画笔
    private Paint mTextPaint;
    // 圆形颜色
    private int mCircleColor;
    // 圆环颜色
    private int mRingColor;
    // 半径
    private float mRadius;
    // 圆环半径
    private float mRingRadius;
    // 圆环宽度
    private float mStrokeWidth;
    // 圆之间的缝隙宽度
    private float mGap;
    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;

    private float mTextWidth;
    private float mTextHeight;
    private int mProgress;
    private RectF mOval;

    private int imageWidth,imageHeight;
    private int mTextSize;
    private  String mTextStr = "结束";
    private Rect mTextBound;


    public LongClickToFinishView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 获取自定义的属性
//        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_end_running);
//        imageHeight = bitmap.getHeight();
//        imageWidth = bitmap.getWidth();
        initAttrs(context, attrs);
        initVariable();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LongClickToFinishView, 0, 0);
        int totalAttributes = typedArray.getIndexCount();
//        mRadius = typedArray.getDimension(R.styleable.LongClickToFinishView_radius, 80);
//        mStrokeWidth = typedArray.getDimension(R.styleable.LongClickToFinishView_strokeWidth, 10);
//        mTextSize =  (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
//        mRingColor = typedArray.getColor(R.styleable.LongClickToFinishView_ringColor, 0xFFFFFFFF);
//        mCircleColor = typedArray.getColor(R.styleable.LongClickToFinishView_circleColor, 0xFFFFFFFF);
//        mGap = typedArray.getDimension(R.styleable.LongClickToFinishView_gap,0);

        //注意：如果用户在布局的XML里面并未设置以下的某个属性，那么下面对应的该属性的赋值语句并不会执行，导致其默认值为0或false等，而不是下面赋值语句中的默认值
        for (int i = 0 ; i<totalAttributes ;i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.LongClickToFinishView_radius:
                    mRadius = typedArray.getDimension(R.styleable.LongClickToFinishView_radius, 80);
                    break;
                case R.styleable.LongClickToFinishView_strokeWidth:
                    mStrokeWidth = typedArray.getDimension(R.styleable.LongClickToFinishView_strokeWidth, 10);
                    break;
                case R.styleable.LongClickToFinishView_centralTextSize:
                    mTextSize = typedArray.getDimensionPixelSize(index, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.LongClickToFinishView_circleColor:
                    mCircleColor = typedArray.getColor(R.styleable.LongClickToFinishView_circleColor, 0xFFFFFFFF);
                    break;
                case R.styleable.LongClickToFinishView_ringColor:
                    mRingColor = typedArray.getColor(R.styleable.LongClickToFinishView_ringColor, 0xFFFFFFFF);
                    break;
                case R.styleable.LongClickToFinishView_gap:
                    mGap = typedArray.getDimension(R.styleable.LongClickToFinishView_gap,0);
                    break;
                    default:
                        break;
            }
        }
        mRingRadius = mRadius + mGap + mStrokeWidth/2;
        typedArray.recycle();//注意： 在typedArray 调用recycle() 就不能再调用其任何方法了，例如typedArray.getIndexCount() 否则会报异常
//        Logger.e("initAttrs circle radius--"+mRadius+"--stroke width is "+mStrokeWidth+"--ring radius "+mRingRadius+"--index count "+totalAttributes);

    }

    private void initVariable() {
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setARGB(255, 255, 255, 255);
        mTextPaint.setTextSize(mTextSize);
        mTextBound = new Rect();
        mTextPaint.getTextBounds(mTextStr, 0, mTextStr.length(), mTextBound);

//        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
//        mTextHeight = (int) Math.ceil(fm.descent - fm.ascent);

        mOval = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mXCenter = getWidth() / 2;
        mYCenter = getHeight() / 2;
        canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
//        mTextWidth = mTextPaint.measureText(mTextStr, 0, mTextStr.length());
//        canvas.drawText(txt, mXCenter - mTextWidth / 2, mYCenter + mTextHeight / 4, mTextPaint);
        canvas.drawText(mTextStr,  mXCenter - mTextBound.width() / 2, mYCenter+ mTextBound.height() / 2, mTextPaint);
        if (mProgress > 0 ) {
            mOval.left = (mXCenter - mRingRadius);
            mOval.top = (mYCenter - mRingRadius);
            mOval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            mOval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            canvas.drawArc(mOval, -90, ( mProgress / MAX_PROGRESS) * 360, false, mRingPaint);
        }
        if(mProgress == 100){
            mProgress = 0;
            postInvalidateDelayed(200);//延迟是为了保证用户能看到画完整个圆
        }
    }

    public void setProgress(int progress) {
        mProgress = progress;
        postInvalidate();
    }

}
